package com.kanche.bigbaby.collection;

import android.content.Context;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.kanche.bigbaby.R;
import com.kanche.bigbaby.AppConfig;
import com.kanche.bigbaby.collection.domain.ApiListResult;
import com.kanche.bigbaby.collection.domain.Gallery;
import com.kanche.bigbaby.collection.domain.Vehicle;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.squareup.okhttp.OkHttpClient;

import java.util.Map;
import java.util.concurrent.TimeUnit;

import retrofit.Callback;
import retrofit.RequestInterceptor;
import retrofit.RestAdapter;
import retrofit.client.OkClient;
import retrofit.converter.GsonConverter;
import retrofit.http.Body;
import retrofit.http.GET;
import retrofit.http.Multipart;
import retrofit.http.POST;
import retrofit.http.Part;
import retrofit.http.QueryMap;
import retrofit.mime.TypedFile;

//import com.kkche.bss.BigBabyApplication;
//import com.kkche.bss.R;
//import com.kkche.bss.collection.dialog.SetToOfflineDialog;
//import com.kkche.bss.collection.domain.ApiListResult;
//import com.kkche.bss.collection.domain.ApiResult;
//import com.kkche.bss.collection.domain.DistributionJobsPage;
//import com.kkche.bss.collection.domain.DistributionRequest;
//import com.kkche.bss.collection.domain.Gallery;
//import com.kkche.bss.collection.domain.PromoteVehicle;
//import com.kkche.bss.collection.domain.ShareAccount;
//import com.kkche.bss.collection.domain.SubstituteCreateStartLocalRequest;
//import com.kkche.bss.collection.domain.SubstituteCreateStartRequest;
//import com.kkche.bss.collection.domain.Vehicle;
//import com.kkche.bss.collection.domain.VehicleModelsDBInfo;
//import com.kkche.bss.domain.AppVersion;
//import com.kkche.bss.domain.AuthRequest;
//import com.kkche.bss.domain.AuthResponse;
//import com.kkche.bss.domain.Compact;
//import com.kkche.bss.domain.CustomerTicket;
//import com.kkche.bss.domain.GeoInfoResponse;
//import com.kkche.bss.domain.Merchant;
//import com.kkche.bss.domain.Page;
//import com.kkche.bss.domain.SearchMember;
//import com.kkche.bss.domain.SubstituteUser;
//import com.kkche.bss.domain.SystemVariable;
//import com.kkche.bss.domain.WorkRecord;

/**
 * Created by nealmi on 6/18/14.
 */
public interface BSSService {

//
//    @POST("/substitute/task/remove/start")
//    void promoteVehicle_xiajia(@Body Map<String, String> substituteTaskId, Callback<ApiResult> callback);
//    @POST("/substitute/task/price/start")
//    void promoteVehicle_gaijia(@Body Map<String, String> substituteTaskId, Callback<ApiResult> callback);
//    @POST("/substitute/task/create/start")
//    void promoteVehicle_fenfa(@Body SubstituteCreateStartRequest s, Callback<ApiResult> callback);
//    @POST("/vehicle/create_substitute_start")
//    void localVehicle_fenfa(@Body SubstituteCreateStartLocalRequest s, Callback<ApiResult> callback);
//
//
//    @POST("/share/account/create")
//    void createShareAccount(@Body ShareAccount account, Callback<ShareAccount> callback);
//
//    @POST("/share/account/update")
//    void updateShareAccount(@Body ShareAccount account, Callback<ShareAccount> callback);
//
//
//    @GET("/share/job/search")
//    void retrieveDistributionJobs(@QueryMap Map<String, String> params, Callback<DistributionJobsPage> callback);
//
//    @GET("/external_vehicle/spec/select?step=3")
//    void findExternalVehicleModels(@QueryMap Map<String, String> parameters, Callback<Map<String, List<Map<String, Object>>>> callback);
//
//    @GET("/external_vehicle/spec/select?step=2")
//    void findExternalVehicleSeries(@QueryMap Map<String, String> parameters, Callback<Map<String, List<Map<String, Object>>>> callback);
//
//    @GET("/external_vehicle/spec/select?step=1")
//    void findExternalVehicleBrands(@QueryMap Map<String, String> parameters, Callback<Map<String, List<String>>> callback);
//
//
//    @GET("/share/availability/get")
//    void retrieveShareAvailabilities(Callback<Map<String, List<ShareAccount>>> callback);
//
//    @GET("/share/account/list?page=0&size=100&accountType=private")
//    void findShareAccounts(@QueryMap Map<String, String> params, Callback<Page<ShareAccount>> callback);
//
//    //    @GET("/share/job/search")
////    void retrieveDistributionJobs(@QueryMap Map<String, String> params, Callback<DistributionJobsPage> callback);
//    @POST("/share/job/restart")
//    void restartDistributionJob(@Body Map<String, String> parameters, Callback<Object> callback);
//
//    @POST("/share/job/send")
//    void distributeVehicle(@Body DistributionRequest request, Callback<ApiListResult> callback);
//
//    @POST("/vehicle/offline")
//    void updateVehicleToOffline(@Body SetToOfflineDialog.OfflineParam body, Callback<Vehicle> callback);
//
//    @POST("/vehicle/delete")
//    void deleteVehicle(@Body Map<String, String> vehicle, Callback<ApiResult> callback);
//
//    @GET("/vehicle/list_all")
//    void findVehicles(@QueryMap Map<String, String> parameters, Callback<Page<Vehicle>> callback);
//
//    @GET("/substitute/task/list")
//    void getAllPromoteList(@QueryMap Map<String, String> parameters, Callback<Page<PromoteVehicle>> callback);
//
//    @POST("/vehicle/adjust_price")
//    void adjustVehiclePrice(@Body Map<String, Object> body, Callback<Vehicle> callback);
//
//    @GET("/user/summary")
//    void getUserSummary(@QueryMap Map<String, String> parameters, Callback<com.kkche.bss.domain.User> callback);
//
    @POST("/vehicle/create")
    void createVehicle(@Body Vehicle vehicle, Callback<Vehicle> callback);
//
    @POST("/vehicle/create_substitute")
    void uploadAndSubstituteVehicle(@Body Vehicle vehicle, Callback<Vehicle> callback);
//
    @POST("/vehicle/create_share")
    void uploadAndDistributeVehicle(@Body Map<String, Object> vehicle, Callback<Vehicle> callback);
//
    @POST("/gallery/create")
    void createGallery(@Body Gallery gallery, Callback<Gallery> callback);
//
    @POST("/vehicle/update")
    void updateVehicle(@Body Vehicle vehicle, Callback<Vehicle> callback);
//
//    @POST("/bss/sign_in")
//    void login(@Body AuthRequest authRequest, Callback<AuthResponse> callback);
//
//    @GET("/system/variable/list?page=0&size=1000")
//    void getSystemVariable(Callback<SystemVariable> callback);
//
//    @GET("/merchant/search")
//    void getMerchantList(@QueryMap Map<String, String> params, Callback<Page<Merchant>> callback);
//
//    @POST("/merchant/take")
//    void takeMerchant(@Body Map<String, String> body, Callback<Merchant> callback);
//
//    @POST("/merchant/return")
//    void returnMerchant(@Body Map<String, String> body, Callback<Merchant> callback);
//
//    @POST("/merchant/assign")
//    void distributeMerchant(@Body Map<String, String> body, Callback<Merchant> callback);
//
//    @POST("/merchant/substitute/open")
//    void openSubstituteService(@Body SubstituteUser.OpenSubRequest openSubRequest, Callback<Merchant> callback);
//
//    @POST("/merchant/substitute/close")
//    void closeSubstituteService(@Body Map<String, String> body, Callback<Merchant> callback);
//
//    @POST("/merchant/weidian/open")
//    void openWeiDian(@Body Map<String, String> body, Callback<Merchant> callback);
//
//    @POST("/merchant/weidian/close")
//    void closeWeiDian(@Body Map<String, String> body, Callback<Merchant> callback);
//
//    @GET("/es/user/search")
//    void getMemberList(@QueryMap Map<String, String> params, Callback<SearchMember> callback);
//
//    @POST("/merchant/user/add")
//    void addMerchantUser(@Body Map<String, String> body, Callback<Merchant> callback);
//
//    @POST("/merchant/user/remove")
//    void removeMerchantUser(@Body Map<String, String> body, Callback<Merchant> callback);
//
//    @GET("/merchant/work_record/list")
//    void getWorkRecordList(@QueryMap Map<String, String> params, Callback<Page<WorkRecord>> callback);
//
//    @POST("/merchant/work_record/create")
//    void createWorkRecord(@Body WorkRecord params, Callback<Object> callback);
//
//    @GET("/contract/list")
//    void getContractList(@QueryMap Map<String, String> params, Callback<Page<Compact>> callback);
//
//    @POST("/contract/create")
//    void createCompact(@Body Compact params, Callback<Object> callback);
//
    @Multipart
    @POST("/upload/picture")
    void uploadPhoto(@Part("file") TypedFile photo, @Part("resize") String resize, Callback<Map<String, String>> callback);
//
//
    @GET("/vehicle/spec/list_by_vin")
    void findVehiclesByVinRecognition(@QueryMap Map<String, String> parameters, Callback<ApiListResult> callback);
//
    @POST("/gallery/update")
    void updateGallery(@Body Gallery gallery, Callback<Gallery> callback);
//
    @Multipart
    @POST("/upload/picture")
    void uploadAvatar(@Part("file") TypedFile photo, @Part("resize") String resize, Callback<Map<String, String>> callback);
//
//    @GET("/area/select")
//    void findAreas(@QueryMap Map<String, String> parameters, Callback<List<Map<String, String>>> callback);
//
//    @POST("/merchant/update")
//    void updateMerchant(@Body Merchant params, Callback<Merchant> callback);
//
//    @POST("/merchant/create")
//    void createMerchant(@Body Merchant params, Callback<Merchant> callback);
//
//    @GET("/sales_support/get_verification_code")
//    void getVerificationCode(@QueryMap Map<String, String> params, Callback<Map<String, String>> callback);
//
//    @GET("/geo/decode")
//    void decodeGeoInfo(@QueryMap Map<String, String> params, Callback<GeoInfoResponse> callback);
//
//
//    @GET("/mobile_app/system")
//    void retrieveSystemInfo(@QueryMap Map<String, String> parameters, Callback<AppVersion> callback);
//
//    @GET("/customer/ticket/search")
//    void getCustomerTicketList(@QueryMap Map<String, String> parameters, Callback<Page<CustomerTicket>> callback);
//
//    @GET("/vehicle/spec/get_package_info")
//    void getVehicleModelsDBInfo(Callback<VehicleModelsDBInfo> callback);
//
//    @GET("/vehicle/list_reject")
//    void rejectVehicleList(@QueryMap Map<String, String> body, Callback<Page<Vehicle>> callback);

    public class CurrentAppConfig implements AppConfig {


        public static final String TENCENT_APP_ID = "1102352264";

        public static final boolean CONSIDER_EXIF_PARAMS = true;
        public static DisplayImageOptions defaultImageLoadOptions = new DisplayImageOptions.Builder()
                .cacheInMemory(true)
                .cacheOnDisk(true)
                .considerExifParams(BSSService.CurrentAppConfig.CONSIDER_EXIF_PARAMS)
                .build();
        public static DisplayImageOptions largePlaceholderOptions = new DisplayImageOptions.Builder()
                .cloneFrom(defaultImageLoadOptions)
                .showImageOnFail(R.drawable.place_holder_large)
                .showImageOnLoading(R.drawable.place_holder_large)
                .build();
        public static DisplayImageOptions mediumPlaceholderOptions = new DisplayImageOptions.Builder()
                .cloneFrom(defaultImageLoadOptions)
                .showImageOnFail(R.drawable.place_holder_medium)
                .showImageOnLoading(R.drawable.place_holder_medium)
                .build();
        public static DisplayImageOptions smallPlaceholderOptions = new DisplayImageOptions.Builder()
                .cloneFrom(defaultImageLoadOptions)
                .showImageOnFail(R.drawable.place_holder_small)
                .showImageOnLoading(R.drawable.place_holder_small)
                .build();
    }


    public static class Creator {
        private static BSSService bssService;

        public static BSSService create() {
            return create(null, null, true);
        }

        public static BSSService create(String endpoint) {
            return create(endpoint, null, true);
        }

        public static BSSService create(Context context) {
            return create(null, context, true);
        }

        public static BSSService create(Context context, boolean requireFuckingParams) {
            return create(null, context, requireFuckingParams);
        }


        public static BSSService create(String endpoint, Context context, boolean requireFuckingParams) {
            String apiEndpoint;
            if (!StringUtils.isHttpPath(endpoint)) {
                apiEndpoint = AppConfig.SERVER_END_POINT;
            } else {
                apiEndpoint = endpoint;
            }


            String t = null;
//            if (context != null) {
//                BigBabyApplication bssApplication = (BigBabyApplication) context.getApplicationContext();
//                t = bssApplication.getBBSDBHelperInstance().findUserToken();
//            }


            final String token = t;
            RequestInterceptor requestInterceptor = new RequestInterceptor() {
                @Override
                public void intercept(RequestFacade request) {
                    request.addHeader("User-Agent", "Kanche Merchant App - Android");
                    if (StringUtils.hasText(token)) {
                        request.addHeader("X-AUTH-TOKEN", token);
                    }
                }
            };

            Gson gson = new GsonBuilder()
                    .create();
            OkHttpClient okHttpClient = new OkHttpClient();
            okHttpClient.setReadTimeout(120, TimeUnit.SECONDS);
            okHttpClient.setConnectTimeout(30, TimeUnit.SECONDS);
            OkClient okClient = new OkClient(okHttpClient);

            RestAdapter.Builder builder = new RestAdapter.Builder()
                    .setEndpoint(apiEndpoint)
                    .setLogLevel(AppConfig.RETROFIT_LOG_LEVEL)
                    .setConverter(new GsonConverter(gson))
                    .setClient(okClient);

            if (requireFuckingParams) {
                builder.setRequestInterceptor(requestInterceptor);
            }

            bssService = builder.build().create(BSSService.class);
            return bssService;
        }
    }

}
